﻿var current_op_type;

$(function(){
    //初始化表格数据
    dataTable = $('#dg').treegrid({
        fit: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        pagination: false,
        idField: 'orgCode', treeField: 'orgName', iconCls: 'icon-ok',
        animate: true, collapsible: true, fitColumns: true, 
        url: _global_web_app_path+'/system/org_info/page',
        method: 'get',
        toolbar: '#tb',
        border: false,
        frozenColumns:[[
                        {title:'组织机构编码',field:'orgCode',width:200, sortable: false,
                            formatter:function(value){
                                return '<span style="color:red">'+value+'</span>';
                            }
                        }
                    ]],        
        columns:  [[
                    { field: 'orgName', title: '组织机构名称', width: 180, sortable: false },
                    { field: 'parentOrgCode', title: '上级单位编码', width: 180, sortable: false }
                    ]]
    });
     
    $('#btn_add').click(function(){add_new_data()});
    
    $('#btn_edit').click(function(){edit_data()});

    $('#btn_delete').click(function(){delete_data()});
    
    $('#btn_query').click(function(){query_data()});
    
   var dialogObj = $('#dd').dialog({
        closed: true,
        cache: false,
        modal: true,
        buttons: [{
            text: '保存',
            iconCls: 'icon-ok',
            handler:function(){
            	
            	var valid_status=$("#edit_form").form("validate");
            	if (!valid_status){
            		return ;
            	}
            	var postData =$("#edit_form").serialize();
                    //ajax调用
                    $.ajax({
                        type: "post",
                        dataType: "json",
                        url:  (current_op_type=='add')?_global_web_app_path+"/system/org_info/create":_global_web_app_path+"/system/org_info/edit",
                        data: postData,
                        error: function (e) {
                            $.messager.alert('错误', e.responseText, 'error');
                        },
                        success: function (data) {
                            $.messager.alert('提示', data.msg);
                            dialogObj.dialog('close');
                            $('#dg').treegrid("reload");
                        }
                    });
            	
            }
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#dd').dialog('close');
            }
        }]
    });

});

function add_new_data(){
	current_op_type='add';
	 var selected_row = $('#dg').datagrid('getSelected');
	 if (!selected_row) {
	        $.messager.alert('提示', '请选择要新增子级单位的上级单位信息', 'error');
	        return;
	}	
	$("#parentOrgCode").val(selected_row.orgCode);
	$("#lbl_parent_node_name").html("上级单位名称:"+selected_row.orgName);
	$('#dd').dialog('open');
}

function edit_data(){
	current_op_type='edit';
	 var selected_row = $('#dg').datagrid('getSelected');
	 if (!selected_row) {
	        $.messager.alert('提示', '请选择需要编辑的数据信息', 'error');
	        return;
	}
	$("#lbl_parent_node_name").html("");
	$('#edit_form').form('load',selected_row);
	$('#dd').dialog('open');
}


function format_data_status(val,row){
	if (true==val){
		return "启用";
	}else{
		return "停用";
	}
}

function delete_data(){
	 var selected_row = $('#dg').datagrid('getSelected');
	 if (!selected_row) {
	        $.messager.alert('提示', '请选择需要删除的数据信息', 'error');
	        return;
	}
    //删除之前给出确认提示
	$.messager.confirm('提示','真的要删除当前选中的数据吗?',function(r){
	   if (r){
			 //ajax调用
		     $.ajax({
		         type: "post",
		         dataType: "json",
		         url:  _global_web_app_path+"/system/org_info/delete",
		         data: {orgCode:selected_row.orgCode},
		         error: function (e) {
		             $.messager.alert('错误', e.responseText, 'error');
		         },
		         success: function (data) {
		             $.messager.alert('提示', data.msg);
		             $('#dg').treegrid("reload");
		         }
		     });	 
	    }
	});
	 
}

function query_data(){
	var query_data_param={
			orgCode: $('#query_org_code').val(),
			orgName: $('#query_org_name').val()			
	};
	
	$('#dg').treegrid('reload', query_data_param);
}